<template>
  <div class="login-bg">
    <div class="login-card">
      <div class="login-title">用户注册</div>
      <div class="login-subtitle">创建您的账户</div>
      <el-form ref="registerFormRef" :model="registerForm" :rules="registerRules" class="register-form" autocomplete="on">
        <el-form-item prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名" prefix-icon="User" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="registerForm.password" :type="passwordVisible ? 'text' : 'password'" placeholder="请输入密码" prefix-icon="Lock" show-password />
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" :type="confirmPasswordVisible ? 'text' : 'password'" placeholder="请确认密码" prefix-icon="Lock" show-password />
        </el-form-item>
        <el-form-item prop="displayName">
          <el-input v-model="registerForm.displayName" placeholder="请输入昵称" prefix-icon="User" />
        </el-form-item>
        <el-form-item prop="phone">
          <el-input v-model="registerForm.phone" placeholder="请输入手机号" prefix-icon="User" />
        </el-form-item>
        <el-form-item prop="email">
          <el-input v-model="registerForm.email" placeholder="请输入邮箱" prefix-icon="User" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="userStore.loading" style="width: 100%" @click="handleRegister">注册</el-button>
        </el-form-item>
      </el-form>
      <div class="login-footer">
        <span>已有账户？<el-link type="primary" @click="goToLogin">立即登录</el-link></span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()
const registerFormRef = ref(null)
const passwordVisible = ref(false)
const confirmPasswordVisible = ref(false)

const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  displayName: '',
  phone: '',
  email: ''
})

const validateConfirmPassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请确认密码'))
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const registerRules = {
  username: [
    { required: true, trigger: 'blur', message: '请输入用户名' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, trigger: 'blur', message: '请输入密码' },
    { min: 6, message: '密码长度不能少于 6 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, trigger: 'blur', validator: validateConfirmPassword }
  ],
  displayName: [
    { required: true, trigger: 'blur', message: '请输入昵称' }
  ],
  phone: [
    { required: true, trigger: 'blur', message: '请输入手机号' },
    { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
  ],
  email: [
    { required: true, trigger: 'blur', message: '请输入邮箱' },
    { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
  ]
}

const handleRegister = async () => {
  if (!registerFormRef.value) return
  try {
    await registerFormRef.value.validate()
    const result = await userStore.register({
      username: registerForm.username,
      password: registerForm.password,
      displayName: registerForm.displayName,
      phone: registerForm.phone,
      email: registerForm.email
    })
    if (result.success) {
      router.push('/login')
    }
  } catch (error) {
    console.error('注册失败:', error)
  }
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.login-bg {
  min-height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #e0ffd6 0%, #b2f7ef 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  width: 400px;
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.12);
  padding: 40px 36px 32px 36px;
  display: flex;
  flex-direction: column;
  align-items: center;
  animation: fadeIn 0.8s;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(40px);}
  to { opacity: 1; transform: translateY(0);}
}
.login-title {
  font-size: 2rem;
  font-weight: 700;
  color: #222;
  margin-bottom: 12px;
  letter-spacing: 2px;
  text-align: center;
}
.login-subtitle {
  font-size: 1rem;
  color: #888;
  margin-bottom: 24px;
  text-align: center;
}
.el-form {
  width: 100%;
}
.el-input__wrapper, .el-input {
  border-radius: 8px !important;
}
.el-button {
  border-radius: 8px;
  font-size: 16px;
  height: 44px;
}
.el-button.is-primary {
  background: linear-gradient(90deg, #4facfe 0%, #00f2fe 100%);
  border: none;
}
.login-footer {
  width: 100%;
  display: flex;
  justify-content: center;
  margin-top: 18px;
  font-size: 15px;
}
.login-footer .el-link {
  font-size: 15px;
}
@media (max-width: 600px) {
  .login-card {
    width: 95vw;
    padding: 24px 8px 16px 8px;
  }
  .login-title {
    font-size: 1.3rem;
  }
}
</style>
